﻿using System;
using System.Collections.Generic;
using System.Linq;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit.Electric.Common;

namespace Model
{
	public class HYConduitAutomaticConnect : HYCmdEntry
	{
		public HYConduitAutomaticConnect(ExternalCommandData commandData) : base(commandData)
		{
		}

		public bool Execute()
		{
			Transaction transaction = null;
			bool result;
			try
			{
				IList<Conduit> list = new List<Conduit>();
				ISelectionFilter selectionFilter = new RevitFilter.ConduitFilter();
				HYConduitConnect hyconduitConnect = new HYConduitConnect();
				IList<Element> list2 = new List<Element>();
				string text = "自动连接";
				int num = 0;
				transaction = new Transaction(this.m_doc, text);
				transaction.Start();
				for (;;)
				{
					SubTransaction subTransaction = new SubTransaction(this.m_doc);
					try
					{
						if (num != 0)
						{
							RevitVersionFuncs.ClearSelection(this.m_sel);
							list2.Clear();
							list.Clear();
						}
						num++;
						if (this.m_sel.GetElementIds().Count > 0)
						{
							IEnumerator<ElementId> enumerator = this.m_sel.GetElementIds().GetEnumerator();
							{
								while (enumerator.MoveNext())
								{
									ElementId elementId = enumerator.Current;
									list2.Add(this.m_doc.GetElement(elementId));
								}
								goto IL_EE;
							}
						}
						list2 = (from re in this.m_sel.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "请框选所要连接的线管")
						select this.m_doc.GetElement(re.ElementId)).ToList<Element>();
						IL_EE:
						foreach (Element element in list2)
						{
							if (element is Conduit)
							{
								list.Add(element as Conduit);
							}
						}
						subTransaction.Start();
						if (list != null && list.Count >= 2)
						{
							if (!hyconduitConnect.NewSomeConnect(list))
							{
								TaskDialog.Show("盈建科提示", "连接失败。");
							}
							else
							{
								this.m_doc.Regenerate();
							}
						}
						subTransaction.Commit();
					}
					catch (Autodesk.Revit.Exceptions.OperationCanceledException)
					{
						result = true;
						break;
					}
					catch (Exception)
					{
						TaskDialog.Show("盈建科提示", "不构成创建配件的条件。");
					}
					finally
					{
						if (subTransaction != null)
						{
							TransactionStatus status = subTransaction.GetStatus();
							if ((int)status == 1)
							{
								subTransaction.Commit();
							}
							else if ((int)status == 5)
							{
								subTransaction.RollBack();
							}
						}
					}
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = true;
			}
			catch (Exception)
			{
				TaskDialog.Show("盈建科提示", "连接失败，请检查线管位置是否正确。");
				result = false;
			}
			finally
			{
				if (transaction != null)
				{
					TransactionStatus status2 = transaction.GetStatus();
                    if ((int)status2 == 1)
					{
						transaction.Commit();
					}
                    else if ((int)status2 == 5)
					{
						transaction.RollBack();
					}
				}
			}
			return result;
		}

		public void OnFailuresProcessing(object sender, FailuresProcessingEventArgs e)
		{
			FailuresAccessor failuresAccessor = e.GetFailuresAccessor();
			string transactionName = failuresAccessor.GetTransactionName();
			IList<FailureMessageAccessor> failureMessages = failuresAccessor.GetFailureMessages();
			if (failureMessages.Count == 0)
			{
				e.SetProcessingResult((FailureProcessingResult)0);
				return;
			}
			if (transactionName.Equals("自动连接"))
			{
				foreach (FailureMessageAccessor failureMessageAccessor in failureMessages)
				{
					failuresAccessor.ResolveFailure(failureMessageAccessor);
				}
				e.SetProcessingResult((FailureProcessingResult)1);
				return;
			}
			e.SetProcessingResult((FailureProcessingResult)0);
		}
	}
}
